//
//  BankListViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/7.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "BankListViewController.h"
#import "BankListTableViewCell.h"
#import "AddBankViewController.h"

@interface BankListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    int _page;
    int _totailPage;
    UIView * noBodyView;
}

@end

@implementation BankListViewController

-(void)viewWillAppear:(BOOL)animated{
    
     [self getListData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"我的钱包"];
    [self creatTableView];
    [self creatEndView];
    [self getListData];
    noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关内容！" onView:nil];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatEndView{
    
    UIView * endView =[[UIView alloc]init];
    
    if (ISPX) {
        endView.frame = CGRectMake(0, HEIGHT-64-48-34, WIDTH, 48+32);
    }else{
        endView.frame = CGRectMake(0, HEIGHT-64-48, WIDTH, 48);
    }
    endView.backgroundColor = NavBackColor;
    
    UIButton * endBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(0, 0, WIDTH, 48);
    endBtn.backgroundColor = NavBackColor;
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [endBtn setTitle:@"添加银行卡" forState:UIControlStateNormal];
    [endBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [endView addSubview:endBtn];
    [self.view addSubview:endView];
}

-(void)addBtnClick{
    AddBankViewController * add = [[AddBankViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[BankListTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[BankListTableViewCell reuseIdentifier]];
    
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getListData];
      
    }];
    _tableView.mj_header = header;
 
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    BankListTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[BankListTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.bankName.text = dic[@"bank_name"];
    cell.careNumber.text = [NSString stringWithFormat:@"%@  %@",dic[@"bank_username"],dic[@"bank_num"]];
    
    cell.deledateBtn.layer.cornerRadius = 3.f;
    cell.deledateBtn.layer.masksToBounds = YES;
    cell.deledateBtn.layer.borderWidth = 0.5f;
    cell.deledateBtn.layer.borderColor = [UIColor grayColor].CGColor;
    cell.deledateBtn.tag = indexPath.row+100;
    [cell.deledateBtn addTarget:self action:@selector(deledateBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    

    return cell;
}

-(void)deledateBtnClick:(UIButton *)btn{
    NSDictionary * dic = _dataArray[btn.tag - 100];
    
    [self deledateBankRequestFor:dic[@"id"]];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)getListData{
    
        NSDictionary * dic = @{
                               @"mid":USER_DEFAULTGet(@"mid"),
                               @"token":USER_DEFAULTGet(@"token")
                               };
    
        [PostNetWorking postNetWorkingStr:RequestUrlStr(get_bankList_api) andparameters:dic Success:^(id responseObject) {
            SHuiMBHidleYes;
            if (codeSucess) {
               
                
                    if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
    
                        _dataArray = responseObject[@"datas"];
                        
                    }
                dispatch_async(dispatch_get_main_queue(), ^{
    
        if (_dataArray.count>0) {
            noBodyView.hidden =YES;
            
        }else {
          
            noBodyView.hidden =NO;
    
            [self.view addSubview:noBodyView];
        }
        [_tableView.mj_header endRefreshing];
    
        [_tableView reloadData];
                });
    
            }else {
    
                SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            }
    
        } failed:^(NSError *error) {
            SHuiMBHidleYes;
            SHuiMBhudTextTimeStr(@"网络错误请重试！");
        }];
}

-(void)deledateBankRequestFor:(NSString *)idStr{
    
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"id":idStr
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(deledate_bank_api) andparameters:dic Success:^(id responseObject) {
        
        if (codeSucess) {
            
            [self getListData];
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
    } failed:^(NSError *error) {
        SHuiMBhudTextNetError;
    }];
    
}

@end
